<template>
  <div class="app-container">
    <el-radio-group v-model="tabPosition" @change="getList" style="margin-bottom: 30px;">
      <el-radio-button v-for="(item,index) in tabList" :key="index" :label="item.value">{{item.label || ''}}</el-radio-button>
    </el-radio-group>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="部门" prop="deptId">
        <treeselect v-model="queryParams.deptId" :clearable="false" style="width:300px;" :options="deptOptions" :show-count="true" placeholder="请选择部门"/>
      </el-form-item>
      <el-form-item label="查询日期" prop="reportDate">
        <el-date-picker :clearable="false"
          v-model="queryParams.reportDate"
          type="date" value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <h2>已汇报：{{listObj.reportedNum || 0}}</h2>
    <TableCard>
      <el-row :gutter="10" class="mb8">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

        <el-table :border="Global.tableShowBorder" :key="'周工作汇报' + tabPosition" v-loading="loading" :data="listObj.reported" v-if="tabPosition == 2">
          <el-table-column label="周编号" align="center" prop="weekNo"/>
          <el-table-column label="年份" align="center" prop="year"/>
          <el-table-column label="用户名" align="center" prop="userName"/>
          <el-table-column label="昵称" align="center" prop="nickName"/>
          <el-table-column label="部门名称" align="center" prop="deptName"/>
          <el-table-column label="汇报开始日" align="center" prop="dateStart"/>
          <el-table-column label="汇报结束日" align="center" prop="dateEnd"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="{row}">
              <el-button
              type="text"
              @click="handleDetail(row)"
              v-hasPermi="['cooperate:weekReport:query']">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-table :border="Global.tableShowBorder" :key="'日工作汇报' + tabPosition" v-loading="loading" :data="listObj.reported" v-else>
          <el-table-column label="用户名" align="center" prop="userName"/>
          <el-table-column label="昵称" align="center" prop="nickName"/>
          <el-table-column label="部门名称" align="center" prop="deptName"/>
          <el-table-column label="汇报日" align="center" prop="reportDate"/>
          <!-- <el-table-column label="汇报内容" align="center" prop="content">
            <template slot-scope="{row}">
              <span v-html="row.content"></span>
            </template>
          </el-table-column> -->
          <el-table-column label="是否已读" align="center" prop="read">
            <template slot-scope="{row}">
              <dict-tag :options="dict.type.comm_yes_no" :value="row.read"/>
            </template>
          </el-table-column>
          <el-table-column label="是否点赞" align="center" prop="like">
            <template slot-scope="{row}">
              <dict-tag :options="dict.type.comm_yes_no" :value="row.like"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="{row}">
                <el-button
                type="text"
                @click="handleDetail(row)"
                v-hasPermi="['cooperate:dayReport:query1']">查看详情</el-button>
              </template>
            </el-table-column>
        </el-table>
    </TableCard>
    <h2>未汇报：{{listObj.unReportedNum || 0}}</h2>
    <TableCard>

        <el-table :border="Global.tableShowBorder" :key="tabPosition" v-loading="loading" :data="listObj.unReported">
          <el-table-column label="用户名" align="center" prop="userName"/>
          <el-table-column label="昵称" align="center" prop="nickName"/>
          <el-table-column label="部门名称" align="center" prop="deptName"/>
        </el-table>
    </TableCard>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form"  label-width="120px">
        <el-row class="diy-text-sm diy-leading-10" :gutter="10">
          <el-col :span="item.span || 12" v-for="(item,index) in (tabPosition == 2 ? formDetail1 : formDetail)" :key="index">
            <div class="flex_hcneter">
              <svg-icon v-if="item.prop != 'attachmentList'" icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
              <div v-if="item.zidian && item.isCustom" class="flex_hcneter">
                {{item.label}}：<dict-tag :options="dict.type[item.zidian]" :value="form[item.prop]"/>
              </div>
              <div v-else-if="item.html && item.isCustom">
                {{item.label}}：
                <div v-html="form[item.prop]"></div>
              </div>
              <div v-else-if="item.prop == 'images' && item.isCustom">
                <h2>{{item.label}}</h2>
                <el-image  v-for="(item1,index1) in form[item.prop]" :key="index1"
                  style="width: 100px; height: 100px;margin-right:10px;margin-bottom:15px;"
                  :src="item1"
                  :preview-src-list="[item1]">
                </el-image>
              </div>
              <div v-else-if="item.prop == 'attachmentList' && item.isCustom">
                <h2>{{item.label}}</h2>
                <el-row v-for="(item1,index1) in form[item.prop]" :key="index1">
                  <el-col :span="24">
                    <svg-icon  icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
                    文件名称：{{item1.fileName}}
                  </el-col>
                  <el-col :span="24">
                    <div class="flex_hcneter">
                      <svg-icon  icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
                      文件路径：<el-link type="primary" @click="urlLink(item1.url)">{{item1.url}}</el-link>
                    </div>
                  </el-col>
                </el-row>

              </div>
              <div v-else-if="item.prop == 'comments' && item.isCustom" class="flex_flex">
                <div class="flex_hcneter_bet">
                  <h2>{{item.label}}</h2>
                  <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleplAdd"
                    v-hasPermi="['cooperate:reportComment:add']"
                  >新增
                  </el-button>
                </div>
                <el-table :border="Global.tableShowBorder" :data="form[item.prop]">
                  <el-table-column label="用户名" align="center" prop="userName"/>
                  <el-table-column label="昵称" align="center" prop="nickName"/>
                  <el-table-column label="用户头像" align="center" prop="avatar">
                    <template slot-scope="{row}">
                      <el-image
                        style="width: 100px; height: 100px"
                        :src="row.avatar"
                        :preview-src-list="[row.avatar]">
                      </el-image>
                    </template>
                  </el-table-column>
                  <el-table-column label="评论内容" align="center" prop="content">
                    <template slot-scope="{row}">
                      <span v-html="row.content"></span>
                    </template>
                  </el-table-column>
                  <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
                  <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
                      <template slot-scope="{row}">
                        <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-edit"
                          @click="handleplUpdate(row)"
                          v-hasPermi="['cooperate:reportComment:edit']"
                        >修改
                        </el-button>
                        <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-delete"
                          @click="handleplDelete(row)"
                          v-hasPermi="['cooperate:reportComment:remove']"
                        >删除
                        </el-button>
                      </template>
                    </el-table-column>
                </el-table>
              </div>
              <div v-else>{{item.label}}：{{ form[item.prop] }}{{ item.danwei || '' }}</div>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 对话框 -->
    <el-dialog :title="plTitle" :visible.sync="plOpen" width="50%" append-to-body>
        <el-form ref="plForm" :model="plForm" :rules="plRules" label-width="120px">
          <el-form-item label="评论内容" prop="content">
            <el-input v-model="plForm.content" type="textarea" :rows="6" placeholder="请输入评论内容"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="plSubmitForm">确 定</el-button>
          <el-button @click="plOpen = false">取 消</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
import {queryDateReports,queryWeekReports} from "@/api/collaWorryFree/departWorkReport";
import {getReportComment, addReportComment, updateReportComment, delReportComment,} from "@/api/collaWorryFree/comment";
import {treeselect} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getDay} from "@/utils/time";
import {getDayReport,} from "@/api/collaWorryFree/dailyWorkReport";
import {getWeekReport,} from "@/api/collaWorryFree/weeklyWorkReport";
export default {
  name: "DepartWorkReport",
  dicts: ['comm_yes_no','daily_work_report_status',],
  components: {Treeselect},
  data() {
    return {
      showViewer: false,//取证图片显示
      initialIndex:0,//取证图片下标
      srcList:[],//取证图片
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格数据
      list: [],
      listObj:{
        unReported:[],reported:[],
      },
      // 查询参数
      queryParams: {
        deptId:null,reportDate:null,
      },
      txPermission:'cooperate:dayReport:query',//日工作汇报
      yjPermission:'cooperate:weekReport:list',//周工作汇报
      tabPosition:1,
      tabList:[],
      deptOptions:[],//部门数据
      // 表单参数
      form: {},
      plForm: {},
      // 表单校验
      plRules: {
        content: [ {required: true, message: "汇报内容不能为空", trigger: "blur"} ],
      },
      formDetail:[
        {label:'用户名',prop:'userName',},{label:'昵称',prop:'nickName',},{label:'部门名称',prop:'deptName',},{label:'汇报日',prop:'reportDate',},
        {label:'汇报内容',prop:'content',isCustom:true,span:24,html:true,},{label:'状态',prop:'status',zidian:'daily_work_report_status',isCustom:true,},
        {label:'是否已读',prop:'read',zidian:'comm_yes_no',isCustom:true,},{label:'是否点赞',prop:'like',zidian:'comm_yes_no',isCustom:true,},
        {label:'已读数量',prop:'readNum',}, {label:'点赞数量',prop:'likeNum',}, {label:'评论数量',prop:'commentNum',},
        {label:'创建时间',prop:'createTime',},{label:'图片列表',prop:'images',isCustom:true,span:24,},
        {label:'附件列表',prop:'attachmentList',isCustom:true,span:24,},{label:'评论列表',prop:'comments',isCustom:true,span:24,},
      ],
      formDetail1:[
        {label:'周编号',prop:'weekNo',},{label:'年份',prop:'year',},{label:'用户名',prop:'userName',},{label:'昵称',prop:'nickName',},
        {label:'部门名称',prop:'deptName',},{label:'汇报开始日',prop:'dateStart',},{label:'汇报结束日',prop:'dateEnd',},
        {label:'本周完成工作内容',prop:'finishContent',isCustom:true,span:24,html:true,},{label:'下周计划工作内容',prop:'nextWeekPlan',isCustom:true,span:24,html:true,},
        {label:'工作总结',prop:'summary',isCustom:true,span:24,html:true,},{label:'需协调与帮助',prop:'assist',isCustom:true,span:24,html:true,},
        {label:'状态',prop:'status',zidian:'daily_work_report_status',isCustom:true,},
        {label:'是否已读',prop:'read',zidian:'comm_yes_no',isCustom:true,},{label:'是否点赞',prop:'like',zidian:'comm_yes_no',isCustom:true,},
        {label:'已读数量',prop:'readNum',}, {label:'点赞数量',prop:'likeNum',}, {label:'评论数量',prop:'commentNum',},
        {label:'创建时间',prop:'createTime',},{label:'图片列表',prop:'images',isCustom:true,span:24,},
        {label:'附件列表',prop:'attachmentList',isCustom:true,span:24,},{label:'评论列表',prop:'comments',isCustom:true,span:24,},
      ],
      // 弹出层标题
      title: "",
      plTitle: "",
      // 是否显示弹出层
      open: false,
      plOpen: false,
    };
  },
  created() {

    let user = this.$store.state.user || {}
    const permissions = user.permissions
    const flag = !!permissions.find(item => item == this.txPermission)
    const flag1 = !!permissions.find(item => item == this.yjPermission)
    if (flag || permissions[0] == '*:*:*'){
      this.tabPosition = 1
      this.tabList.push({value: 1,label:'日工作汇报'})
    }else if(flag1 || permissions[0] == '*:*:*'){
      this.tabPosition = 2
    }
    if(flag1 || permissions[0] == '*:*:*') this.tabList.push({value: 2,label:'周工作汇报'})
    this.getList();
  },
  methods: {
    handleDetail(row) {//查看详情
      const id = row.id
      this.open = true;
      this.title = "查看详情";
      this.getDetail(id)
    },
    async getDetail(id){
      let api = this.tabPosition == 2 ? getWeekReport : getDayReport
      let response = await api(id)
      let data = response.data || {}
      let images = data.images || ''
      let comments = data.comments || []
      this.form = {
        ...data,attachment:data.attachmentList || [],
        finishContent:this.$changeLine(data.finishContent || ''),
        nextWeekPlan:this.$changeLine(data.nextWeekPlan || ''),
        summary:this.$changeLine(data.summary || ''),
        assist:this.$changeLine(data.assist || ''),
        content:this.$changeLine(data.content || ''),
        comments:comments.map(item=>{
          return {...item,content:this.$changeLine(item.content),}
        }),images:images ? images.split(',') : []
      }
    },
    /** 查询部门下拉树结构 */
    async getTreeselect() {
      let response = await treeselect()
      this.deptOptions = response.data || [];
      let queryParams = {...this.queryParams}
      if(this.deptOptions.length) queryParams.deptId = this.deptOptions[0].id
      this.queryParams = {...queryParams}
      return queryParams.deptId
    },
    getInitime(){
      let day = getDay(-1)
      let queryParams = {...this.queryParams}
      queryParams.reportDate = day
      this.queryParams = {...queryParams}
      return day
    },
    /** 查询列表 */
    async getList() {
      this.loading = true;
      let queryParams = {...this.queryParams}
      if(!queryParams.reportDate) {
        queryParams.reportDate = this.getInitime()
      }

      if( !queryParams.deptId ) queryParams.deptId = await this.getTreeselect()
      if(!this.deptOptions.length) {
        this.loading = false;
        return
      }
      if(this.tabPosition == 2) {
        queryParams.queryDate = queryParams.reportDate
        delete queryParams.reportDate
      }
      let api = this.tabPosition == 2 ? queryWeekReports : queryDateReports
      api(queryParams).then(response => {
        let data = response.data || [];
        this.listObj = {
          ...data,
          unReported:data.unReported || [],reported:data.reported || [],
        }
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    plReset() {
      this.plForm = {};
      this.resetForm("plForm");
    },
    /** 新增按钮操作 */
    handleplAdd() {
      this.plReset();
      this.plForm.reportId = this.form.id
      this.plOpen = true;
      this.plTitle = "添加评论";
    },
    handleplUpdate(row) {
      this.plReset();
      const id = row.id
      getReportComment(id).then(response => {
        let data = response.data || {};
        this.plForm = { ...data, }
        this.plOpen = true;
        this.plTitle = "修改评论";
      });
    },
    plSubmitForm: function () {
      this.$refs["plForm"].validate(valid => {
        if (valid) {
          let plForm = {...this.plForm}
          if (this.plForm.id != undefined) {
            updateReportComment(plForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.plOpen = false;
              this.getDetail(this.form.id);
            });
          } else {
            plForm.category = this.tabPosition
            addReportComment(plForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.plOpen = false;
              this.getDetail(this.form.id);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleplDelete(row) {
      const id = row.id
      this.$modal.confirm('是否确认删除该数据项？').then(function () {
        return delReportComment(id);
      }).then(() => {
        this.getDetail(this.form.id);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>
